/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月20日         Root.Lu
 */

package com.coocaa.launcher.pattern.normal.item;

import java.net.URISyntaxException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppShortCutData;
import com.coocaa.launcher.util.Utils;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

/**
 * TODO (write something)
 *
 * @author lu
 * @version 2014年12月23日
 * @see 参考转向，也就是相关主题
 */
@SuppressLint("ResourceAsColor")
public class AppShortcut extends AppBaseItemView {
    private Intent shortCutIntent = null;
    private Drawable iconDrawable = null;
    public static String viewType = "hot_seat_shortcut";

    public AppShortcut(Context context) {
        super(context, viewType);
        // TODO Auto-generated constructor stub
        update();
    }

    public void setIntent(String i) throws URISyntaxException {
        shortCutIntent = Intent.parseUri(i, 0);
    }

    public void setTitle(String title) {
        titleString = title;
        titleView.setText(title);
    }

    public void setIcon(Drawable drawable) {
        iconDrawable = drawable;
        iconView.setImageDrawable(drawable);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        try {
            mContext.startActivity(shortCutIntent);
        } catch (Exception e) {
            LogUtils.i("" + e);
        }
    }

    @Override
    public void onThemeChange() {
        // TODO Auto-generated method stub
        update();
    }

    private void update() {
        removeAllViews();

        View view = SkinManager.getInstance().getLayout(R.layout.appitemview_appshortcut);
        addView(view);
        iconView = (ImageView) findViewById(R.id.appshortcut_imageview_icon);
        LayoutParams params = (LayoutParams) iconView.getLayoutParams();
        params.height = SkyConstants.Div(144);
        params.width = SkyConstants.Div(144);
        iconView.setLayoutParams(params);

        titleView = (TextView) findViewById(R.id.appshortcut_textview_title);
        titleView.setTextSize(SkyConstants.Dpi(29));
        if (titleString != null)
            setTitle(titleString);
        if (iconDrawable != null)
            setIcon(iconDrawable);
    }

    public void update(AppShortCutData data) {
        this.data = data;
        if (data == null)
            return;
        setTitle(data.getTitle());

        iconDrawable = Utils.loadDrawableFromPath(data.getShortcutIcon());
        setIcon(iconDrawable);
    }
}
